﻿CALL cleanPaths();

DELETE
FROM album
WHERE idAlbum NOT IN (SELECT idAlbum FROM song);

DELETE
FROM albuminfo
WHERE idAlbum NOT IN (SELECT idAlbum FROM album);

DELETE
FROM albuminfosong
WHERE idAlbumInfo NOT IN (SELECT idAlbumInfo FROM albuminfo);

DELETE
FROM exartistsong
WHERE idSong NOT IN (SELECT idSong FROM song);

DELETE
FROM exartistalbum
WHERE idAlbum NOT IN (SELECT idAlbum FROM album);

DELETE
FROM exgenresong
WHERE idAlbum NOT IN (SELECT idAlbum FROM album);

DELETE
FROM exgenrealbum
WHERE idAlbum NOT IN (SELECT idAlbum FROM album);

DELETE
FROM artist
WHERE idArtist NOT IN (
    SELECT idArtist FROM album
    UNION ALL
    SELECT idArtist FROM song
    UNION ALL
    SELECT idArtist FROM discography
    UNION ALL
    SELECT idArtist FROM exartistalbum
    UNION ALL
    SELECT idArtist FROM exartistsong
);

DELETE
FROM artistinfo
WHERE idArtist NOT IN (SELECT idArtist FROM artist);

DELETE
FROM discography
WHERE idArtist NOT IN (SELECT idArtist FROM artist);

DELETE
FROM genre
WHERE idGenre NOT IN (
	SELECT idGenre FROM genre
	UNION ALL
	SELECT idGenre FROM exgenresong
	UNION ALL
	SELECT idGenre FROM exgenrealbum
	UNION ALL
	SELECT idGenre FROM albuminfo
);

DELETE
FROM thumb
WHERE idThumb NOT IN (SELECT idThumb FROM album);
